import wx

# Access Grid Toolkit imports
import agversion
agversion.select(3)
from AccessGrid.DataStoreClient import GetVenueDataStore

import Constants





class RemoteFileBrowser(wx.Dialog):
    """
        This Class is used to show the open file dialog from the menu bar.
    """
    
    def __init__(self, parent, id, title, venueUrl):
        wx.Dialog.__init__(self, parent, id, title)

        sizer = wx.BoxSizer(wx.VERTICAL)

        self.remote_files = wx.ListBox(self,-1)

        bpanel = wx.Panel(self, -1)
        bsizer = wx.BoxSizer(wx.HORIZONTAL)

        okbutton = wx.Button(bpanel, wx.NewId(), 'Ok')
        cancelbutton = wx.Button(bpanel, wx.NewId(), 'Cancel')

        bsizer.Add(okbutton, 1, wx.EXPAND | wx.ALL, 5)
        bsizer.Add(cancelbutton, 1, wx.EXPAND | wx.ALL, 5)

        bpanel.SetSizer(bsizer)
        bsizer.SetSizeHints(bpanel)

        sizer.Add(self.remote_files, 1, wx.EXPAND | wx.ALL, 5)
        sizer.Add(bpanel, 0, wx.EXPAND | wx.ALL, 5)

        self.SetSizer(sizer)
        sizer.SetSizeHints(self)

        self.selected_file = None

        self.populateList(venueUrl)

        wx.EVT_BUTTON(self, okbutton.GetId(), self.OnOk)
        wx.EVT_BUTTON(self, cancelbutton.GetId(), self.OnCancel)
        
    def OnOk(self, event):
        self.selected_file = self.remote_files.GetStringSelection()
        self.Close( True )

    def OnCancel(self, event):
        self.Close( True )

    def populateList(self, venueUrl):
        """
            Fill the dialog with filenames.
            
            Parameters:
                venueUrl: URL of the venue.
        """
        self.remote_files.Clear()
        dsc = GetVenueDataStore( venueUrl, self.GetParent().maincontroller.clientProfile.connectionId )
        files = dsc.QueryMatchingFilesMultiple( Constants.SUPPORTED_FILE_EXTENSIONS )
        self.remote_files.InsertItems(files, 0)

    def getSelectedFile(self):
        return self.selected_file
        
    
class MainFrame(wx.Frame):
    """
        The MainFrame is - you guessed it - the main frame of the program.
        From here you can launch new OSGFrames.
    """
    
    ID_OPENSHAREDFILE = wx.NewId()
    ID_OPENLOCALFILE = wx.NewId()
    ID_EXIT = wx.NewId()
    ID_TIMER = wx.NewId()
    
    def __init__(self, maincontroller, **kwds):
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, None, -1, 'SharedOSG v2', **kwds)
        
        self.maincontroller = maincontroller

        menubar = wx.MenuBar()

        self.fileMenu = wx.Menu()
        self.fileMenu.Append(MainFrame.ID_OPENSHAREDFILE,
                             '&Open ...',
                             'Open file')

        self.fileMenu.AppendSeparator()
        self.fileMenu.Append(MainFrame.ID_EXIT, '&Exit', 'Exit')
        menubar.Append(self.fileMenu, '&File')

        self.SetMenuBar(menubar)

        # begin wxGlade
        self.chFilesOnVenue = wx.Choice(self, -1, choices=[Constants.STR_FILE_LIST_FIRST_ITEM])
        self.btLoad = wx.Button(self, -1, "Load")
        self.label_1 = wx.StaticText(self, -1, "Participants:")
        self.lstParticipants = wx.ListBox(self, -1, choices=[], style=wx.LB_SINGLE|wx.LB_HSCROLL|wx.LB_NEEDED_SB)

        self.__set_properties()
        self.__do_layout()
        # end wxGlade
        
        # Register the callbacks
        wx.EVT_MENU(self, MainFrame.ID_OPENSHAREDFILE, self.OnOpenFile)
        wx.EVT_MENU(self, MainFrame.ID_OPENLOCALFILE, self.OnUploadFile)
        wx.EVT_MENU(self, MainFrame.ID_EXIT, self.OnFileExit)
        self.Bind(wx.EVT_WINDOW_DESTROY, self.maincontroller.OnMainframeDestroy, self)
        self.Bind(wx.EVT_BUTTON, self.OnLoadFile, self.btLoad)  # The load button


    def __set_properties(self):
        self.chFilesOnVenue.SetSelection(0)


    def __do_layout(self):
        # begin wxGlade: MyFrame.__do_layout
        sizer_3 = wx.BoxSizer(wx.VERTICAL)
        sizer_4 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_6 = wx.BoxSizer(wx.VERTICAL)
        sizer_5 = wx.BoxSizer(wx.VERTICAL)
        sizer_7 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_7.Add(self.chFilesOnVenue, 0, 0, 0)
        sizer_7.Add(self.btLoad, 0, wx.LEFT, 3)
        sizer_5.Add(sizer_7, 1, wx.EXPAND, 0)
        sizer_4.Add(sizer_5, 1, wx.EXPAND, 0)
        sizer_6.Add(self.label_1, 0, wx.BOTTOM, 2)
        sizer_6.Add(self.lstParticipants, 0, wx.ALL, 0)
        sizer_4.Add(sizer_6, 1, wx.LEFT|wx.EXPAND, 4)
        sizer_3.Add(sizer_4, 1, wx.EXPAND, 0)
        self.SetSizer(sizer_3)
        sizer_3.Fit(self)
        self.Layout()
        # end wxGlade
        
        
    def OnLoadFile(self, event):
        """
            This callback function is called when the Load button is clicked.
            It triggers the creation of a new OSGFrame.
        """
        selectedFile = self.chFilesOnVenue.GetStringSelection()
        
        # If the selected file is not the first element in the list 
        # (which is no filename but just text), open this selected file.
        if selectedFile != Constants.STR_FILE_LIST_FIRST_ITEM:
            self.maincontroller.OpenFileFromDatastore( selectedFile )
         
         
    def OnOpenFile(self, event):
        """
            This callback function is called when 'Open...' from the menu bar is clicked.
            It opens a dialog, waits for the user to select a file and triggers the
            creation of a new OSGFrame with the selected file.
        """
        browser = RemoteFileBrowser( self,
                                     -1,
                                     'Select File to Load',
                                     self.maincontroller.venueUrl )
        browser.ShowModal()

        fname = browser.getSelectedFile()
        if fname != None:
            self.maincontroller.OpenFileFromDatastore( fname )


    def OnUploadFile(self, event):
        pass


    def OnFileExit(self, event):
        """
            This callback function is called when 'Exit' from the menu bar is clicked.
            It triggers the destroy event of the MainFrame.
        """
        self.Destroy()
